package cn.yzj.utils;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 *
 * @author gzkemays
 * @since 2022/1/24 0:34
 */
public class TimeUtils {
  public static final int MILLIS_LENGTH = 13;
  /**
   * 获取时间戳的日期
   *
   * @param time 时间戳
   * @return 格式化日期
   */
  public static long toMillis(long time) {
    if (String.valueOf(time).length() < MILLIS_LENGTH) {
      time = time * 1000L;
    }
    return time;
  }
  /**
   * 获取时间戳的日期
   *
   * @param time 时间戳
   * @return 格式化日期
   */
  public static String getTimestampData(long time) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
    return sdf.format(toMillis(time));
  }

  /**
   * 返回当前时间戳为秒的 int 数据
   *
   * @return 当前时间戳 -- 精准至秒
   */
  public static Integer getSeconds() {
    long time = System.currentTimeMillis();
    return Integer.parseInt(String.valueOf(time / 1000));
  }
  /**
   * 返回当前时间戳为毫秒的 int 数据
   *
   * @return 当前时间戳 -- 精准至毫秒
   */
  public static long getMillis() {
    return System.currentTimeMillis();
  }

  /**
   * @param month 月份
   * @return 获取某月后的时间戳
   */
  public static long getMonthAfterTimeStamp(int month) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MONTH, +month);
    Date beforeDay = calendar.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sdf.parse(sdf.format(beforeDay), new ParsePosition(0)).getTime() / 1000;
  }

  /**
   * @param seconds 秒数
   * @return 获取 {@code seconds} 后的时间戳
   */
  public static long getSecondsAfterTimeStamp(int seconds) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.SECOND, +seconds);
    Date beforeDay = calendar.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sdf.parse(sdf.format(beforeDay), new ParsePosition(0)).getTime() / 1000;
  }
  /**
   * @param minute 分钟
   * @return 获取 {@code minute} 后的时间戳
   */
  public static long getMinuteAfterTimeStamp(int minute) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MINUTE, +minute);
    Date beforeDay = calendar.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sdf.parse(sdf.format(beforeDay), new ParsePosition(0)).getTime() / 1000;
  }

  /**
   * @param hours 小时
   * @return 获取 {@code hours} 后的时间戳
   */
  public static long getHoursAfterTimeStamp(int hours) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.HOUR, +hours);
    Date beforeDay = calendar.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sdf.parse(sdf.format(beforeDay), new ParsePosition(0)).getTime() / 1000;
  }

  /**
   * @param day 天数
   * @return 获取 {@code day} 后的时间戳
   */
  public static long getDaysAfterTimeStamp(int day) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, +day);
    Date beforeDay = calendar.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sdf.parse(sdf.format(beforeDay), new ParsePosition(0)).getTime() / 1000;
  }

  /**
   * 与当前时间进行比较
   * @param time 时间
   * @return {@code time} > {@link Date}
   */
  public static Boolean compareCurrentTime(String time) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return compareTime(time, sdf.format(new Date()), "yyyy-MM-dd HH:mm:ss");
  }

  /**
   * 比较时间戳字符串
   *
   * @param time1 时间1
   * @param time2 时间2
   * @return {@code time1} > {@code time2}
   */
  public static Boolean compareTime(String time1, String time2) {
    return compareTime(time1, time2, "yyyy-MM-dd HH:mm:ss");
  }

  /**
   * 比较时间戳字符串
   *
   * @param time1 时间1
   * @param time2 时间2
   * @param format 时间转换规则
   * @return {@code time1} > {@code time2}
   */
  public static Boolean compareTime(String time1, String time2, String format) {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    try {
      Date parse = sdf.parse(time1);
      Date parse1 = sdf.parse(time2);
      return parse.getTime() > parse1.getTime();
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 时间戳转换
   *
   * @param year 年
   * @param month 月
   * @param day 日
   * @param hours 小时
   * @param min 分钟
   * @param second 秒
   * @return 时间戳
   */
  public static long buildTimestamp(int year, int month, int day, int hours, int min, int second) {
    return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
            .parse(
                year + "-" + month + "-" + day + " " + hours + ":" + min + ":" + second,
                new ParsePosition(0))
            .getTime()
        / 1000;
  }
}
